/*
  * Description: common definition.
 */

#ifndef COMMON_H
#define COMMON_H

#include <string>

constexpr uint64_t KB_BYTES = 1024;
constexpr uint64_t MB_BYTES = 1024 * 1024;
constexpr uint64_t GB_BYTES = 1024 * 1024 * 1024;

constexpr uint32_t VM_CPU_MIN_NUM = 2;
constexpr uint32_t VM_MEMORY_MIN_SIZE = 4 * 1024; // 单位MB
constexpr uint32_t VM_MEMORY_MAX_SIZE = 6 * 1024; // 单位MB
constexpr uint32_t VM_DISK_MIN_SIZE = 64 * 1024;  // 单位MB
constexpr uint32_t VM_DISK_MAX_SIZE = 512 * 1024; // 单位MB
constexpr float VM_MAX_AVAIL_RATIO = 0.8; // VM可用内存及磁盘最大为Host的80%，向下取整

constexpr int PORT_ZERO = 0;
constexpr int THREAD_NUMS_UI_RECEIVER = 5;
constexpr int THREAD_NUMS_GUEST_RECEIVER = 5;
constexpr int THREAD_NUMS_UI_HANDLE = 3;

constexpr uint32_t DEFAULT_SOCKET_ACCESS = 0600; // socket文件权限，8进制

constexpr int INVALID_FD = -1;

const std::string DESKTOP_APP_ENGINE_PROCESS_NAME = "[VMAssistant]"; // 虚拟机助手进程名
const std::string VM_MANAGER_PROCESS_NAME = "vm_manager";
const std::string QCOW_DISK_ID = "system";

constexpr int INVALID_WD = -1; // invalid watch descriptor
enum class MonitorState {
    STOPPED,
    RUNNING,
};
constexpr int INVALID_UID = -1;

constexpr int MAX_NET_RES_NUM = 5;
const std::string RES_ITEM_PREFIX = "Res";
const std::string RES_KEY_USER_ID = "UserId";
const std::string RES_KEY_TAP_NAME = "TapName";
const std::string RES_KEY_IP_ADDR = "IpAddr";
const std::string RES_KEY_MAC_ADDR = "MacAddr";
const std::string RES_KEY_NAT_PORT = "NatPort";

constexpr int MAC0 = 0;
constexpr int MAC1 = 1;
constexpr int MAC2 = 2;
constexpr int MAC3 = 3;
constexpr int MAC4 = 4;
constexpr int MAC5 = 5;

constexpr int MAC_ADDR_BYTE_LEN = 6;

constexpr uint64_t DISK_SAFE_SPACE = 20 * 1024;
constexpr uint64_t DISK_WARNING_SPACE = 8 * 1024;
constexpr uint64_t DISK_DANGER_SPACE = 3 * 1024;

constexpr uint32_t MIN_AVAILABLE_MEMORY_SIZE_GB = 6;
constexpr uint32_t MIN_AVAILABLE_CPU_CORES = 4;
constexpr uint32_t MAX_AVAILABLE_CPU_CORES = 6;
constexpr float PERCENTAGE_OF_MAX_NUM_OF_AVAILABLE_CPU_CORES = 0.8f;
constexpr float PERCENTAGE_OF_MAX_NUM_OF_AVAILABLE_MEMORY_SIZE = 0.75f;
constexpr uint32_t COMPUTE_STEP = 1024;
constexpr float COMPUTE_STEP_F = 1024.0f;
constexpr uint32_t MEMORY_UNIT = 8;

constexpr const char *VM_CONTROL_EVENT_NAME = "SA_VM_CONTROL";
enum class VmControlType {
    INSTALL = 0,
    START,
    STOP,
    DESTORY,
    GUEST_SHUTDOWN,
    FORCE_STOP
};
constexpr const char *SA_VM_DISK_LESS = "SA_VM_DISK_LESS";
enum class VmDiskLessErrCode {
    NONE = 0,
    INSTALL,
    START
};
constexpr const char *SA_VM_MEMORY_LESS = "SA_VM_MEMORY_LESS";
constexpr const char *SA_VM_ALLOCATED_NET_ERROR = "SA_VM_ALLOCATED_NET_ERROR";
constexpr const char *SA_VM_CREATION_PARAM_INVALID = "SA_VM_CREATION_PARAM_INVALID";
constexpr const char *SA_VM_START_PARAM_INVALID = "SA_VM_START_PARAM_INVALID";
enum class VmParamErrCode {
    NONE = 0,
    INVALID_CPU_NUM,
    INVALID_MEMORY_SIZE
};
constexpr const char *SA_VM_MIRROR_ERROR = "SA_VM_MIRROR_ERROR";
enum class VmMirrorErrCode {
    NONE = 0,
    OCCUPIED,
    DAMAGED
};
constexpr const char *SA_VM_KILL_SV_ERROR = "SA_VM_KILL_SV_ERROR";
constexpr const char *SA_VM_INSTALL_CMD_ERROR = "SA_VM_INSTALL_CMD_ERROR";
constexpr const char *SA_VM_START_CMD_ERROR = "SA_VM_START_CMD_ERROR";
constexpr const char *SA_VM_SET_MEMORY = "SA_VM_SET_MEMORY";
enum class VmCmdErrCode {
    NONE = 0,
    CMD_EXEC_FAILED,
    KVM_DISABLED
};

enum class MemorySpec {
    SIXTEEN = 16,
    TWENTYFOUR = 24,
    THIRTYTWO = 32
};

constexpr uint64_t MEMORY_SIZE_EIGHT = 8;
constexpr uint64_t MEMORY_SIZE_TEN = 10;
constexpr uint64_t MEMORY_SIZE_EIGHTEEN = 18;

constexpr const char* APPLICATION_WINDOW_HELPER_EXE_PATH =
    "C:\\ProgramData\\performance\\application_window_helper.exe";
constexpr const char* APPLICATION_WINDOW_HELPER_FILE_PATH =
    "C:\\Users\\%s\\AppData\\Local\\Temp\\applicationWindowResult";

#endif // COMMON_H